package tpcompiladores;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class TablaSimb {
    
    public Hashtable<String,RegTs> table;
    private int index = 1;
    
    public TablaSimb(){
        table = new Hashtable<String,RegTs>();
        //Insert into the hashtable all the reserved keywords
        table.put("if", new RegTs("Reservado",""));
        table.put("then",new RegTs("Reservado",""));
        table.put("else", new RegTs("Reservado",""));
        table.put("print", new RegTs("Reservado",""));
        table.put("var", new RegTs("Reservado",""));
        table.put("array", new RegTs("Reservado",""));
        table.put("double", new RegTs("Reservado",""));
        table.put("do", new RegTs("Reservado",""));
        table.put("until",new RegTs("Reservado",""));       
    }
    
    public void putSymbol(String key, RegTs reg){
        table.put(key, reg);
    }
    
    public RegTs getRegister(String key){
        return table.get(key);
    }
    
    public boolean exists(String key){
        return table.keySet().contains(key);
    }
    
    public int getIndex(){
        return index++;
    } 
    
    public String getCte(String value){
        String key;
        Enumeration<String> keys = table.keys();
        while(keys.hasMoreElements()){
            key = keys.nextElement();
            if(table.get(key).getValue().equals(value))
                return key;
        }
        String newKey = "cte" + getIndex();
        table.put(newKey, new RegTs("CTE DOUBLE",value));
        return newKey;
    }
      
    public void printTable(){
        String key;
        Enumeration<String> keys = table.keys();
        System.out.println("TABLA DE SIMBOLOS");
        System.out.println("-------------------------------");
        while(keys.hasMoreElements()){
            key = keys.nextElement();
            System.out.println("KEY: " + key + " Reg: " + table.get(key).toString());
        }
    }
    
    public Vector<Vector> getRows(){
        String key;
        Vector<Vector> result = new Vector();
        Vector<String> aux;
        Enumeration<String> keys = table.keys();
        while(keys.hasMoreElements()){
            aux = new Vector();
            key = keys.nextElement();
            //aux.add(key);
            aux.add(key);
            aux.add(table.get(key).getType());
            aux.add(table.get(key).getValue());
            result.add(aux);
        }        
        return result;
    }
    
    public String getLim(String key){
        RegTs result = table.get(key);
        int index = result.getType().indexOf(':');
        return result.getType().substring(index+1).trim();   
    }

    public void negativo(String sval) {
        if(exists(sval)){
            RegTs reg = table.get(sval);
            double val = Double.parseDouble(reg.getValue());
            table.remove(sval);
            String res = String.valueOf(val * -1);
            table.put(sval,new RegTs("CTE DOUBLE",res));
            
        }
        
        /*if(exists(sval)){
            table.remove(sval);
            table.put(sval)
            table.put("-" + sval,"CTE DOUBLE");
        } */           
    }
    
   public boolean isVar(String key){
        if(exists(key)){
            RegTs s = table.get(key);
            if(s.getType().equals("DOUBLE"))
                return true;
            else
                return false;
                        
        }
        else return false;
    }
    
    public boolean isArray(String key){
        if(exists(key)){
            RegTs s = table.get(key);
            if(s.getType().contains("ARRAY"))
                return true;
            else
                return false;
                        
        }
        else return false;
    }
}
